<template>
  <div class="hy-table">
    <div class="header">
      <slot name="header">
        <div class="title">
          {{title}}
        </div>
        <div class="handler">
          <slot name="handerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="listData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-bind="childrenProps"
    >
        <!-- <el-table-column prop="name" label="用户名" min-width="180" />
        <el-table-column prop="realname" label="真实姓名" min-width="180" />
        <el-table-column prop="cellphone" label="电话号码"  min-width="180" /> -->
        <el-table-column v-if="showSelectColunm" type="selection" align="center" width="60">
        </el-table-column>
        <el-table-column v-if="showIndexColumn" type="index" label="序号" align="center" width="80">
        </el-table-column>
        <template v-for="propItem in propList" :key="propItem.id">
          <el-table-column v-bind="propItem" align="center" show-overflow-tooltip>
            <template #default="scope">
             <slot :name="propItem.slotName" :row="scope.row">
               {{ scope.row[propItem.prop] }}
             </slot>
            </template>
          </el-table-column>
        </template>
    </el-table>
    <div class="footer" v-if="showFooter">
      <slot name="footer">
         <el-pagination
          :currentPage="page.currentPage"
          :page-size="page.pageSize"
          :page-sizes="[10, 20, 30]"
          :small="small"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  props:{
    title:{
      type:String,
      default:''
    },
    listData:{
      type:Array,
      default:()=>[],
      required:true
    },
    listCount:{
      type:Number,
      default:0
    },
    propList:{
      type:Array,
      default:()=>[],
      required:true
    },
    showIndexColumn:{
      type:Boolean,
      default:false
    },
    showSelectColunm:{
      type:Boolean,
      default:false
    },
    page:{
      type:Object,
      default:()=>({currentPage:0,pageSize:10})
    },
    childrenProps:{
      type:Object,
      default:()=>({})
    },
    showFooter:{
      type:Boolean,
      default:true
    }
  },
  //
  emits:['selectionChange','update:page'],
  setup(props,{emit}) {
    //选择表格行触发回调
    const handleSelectionChange = (value: any) =>{
      // console.log(value)
      emit('selectionChange',value)
    }

    const handleSizeChange = (pageSize: number) => {
      emit('update:page',{...props.page,pageSize})
    }

    const handleCurrentChange = (currentPage: number) => {
      emit('update:page',{...props.page,currentPage})
    }
    return {
      handleSelectionChange,
      handleSizeChange,
      handleCurrentChange,
    }
  }
})
</script>

<style lang="less" scoped>
.header{
  display: flex;
  height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;
  .title{
    font-size: 20px;
    font-weight: 700;
  }
  .handler{
    align-items: center;
  }
}
.footer{
  margin-left: 15rpx;
  margin-top: 20px;
  .el-pagination{
    text-align: right;
  }
}
</style>
